﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data.Linq;
using System.Data;
using VQWEB.common;
namespace VQWEB.administrator.sources
{
    public class EventData
    {
        public core_event GetEventById(int Id)
        {
            using (CoreModelDataContext content = new CoreModelDataContext())
            {
                var query = content.core_events.Where(c => c.event_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new core_event();
            }
        }
        public int InsertOrUpdateContact(core_event obj)
        {
            try
            {
                int Id = 0;
                using (CoreModelDataContext context = new CoreModelDataContext())
                {
                    if (obj.event_id > 0)
                    {
                        core_event flag = GetEventById(obj.event_id);
                        if (flag != null)
                        {
                            context.core_events.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.event_id;
                        }
                    }
                    else
                    {
                        context.core_events.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.event_id;

                    }

                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
        public DataTable ViewEvent()
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_events
                            select new { 
                                event_id = c.event_id,
                                event_title = c.event_title,
                                time_start = c.time_start,
                                time_end = c.time_end,
                                content_start= c.content_start,
                                content_end = c.content_end,
                                event_active = c.event_active
                            };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        }
        public void DeleteContact(List<int> array_id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_events
                            where array_id.Contains(c.event_id)
                            select c;
                if (query.Any())
                {
                    context.core_events.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }

            }

        }
        public void LockUnLockEvent(List<int> array_id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_events
                            where array_id.Contains(c.event_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.event_active = (item.event_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }

        }
    }
}